package org.i_plus.model.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.i_plus.model.ConstellationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;



public class ConstellationHibernateDAO implements ConstellationDAO {
	private SessionFactory sessionFactory;
	String selectAll = "from ConstellationBean";
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<ConstellationBean> selectAll() {
		List<ConstellationBean> result = new ArrayList<ConstellationBean>();
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(selectAll);
		result = query.list();
		return result;
	}
	
	
	@Override
	public ConstellationBean select(String name) {
		Session session = sessionFactory.getCurrentSession();
		ConstellationBean bean = (ConstellationBean) session.get(
				ConstellationBean.class, name);
//		System.out.println(bean.getName());
//		System.out.println(bean.getPic());
//		System.out.println(bean.getIntro());
		return bean;
		// session.close();

	}
	
	@Override
	public void insertConstellation(String name, byte[] pic, String intro,
			String mind_star, String money_star, String health_star,
			String luck_dir, String love_star, String work_star,
			String luck_color, String luck_thing, String mind_word,
			String love_word, String money_word, String work_word) {
		Session session = sessionFactory.getCurrentSession();
//		Transaction tx = session.beginTransaction();
		ConstellationBean bean = new ConstellationBean();
		bean.setName(name);
		bean.setPic(pic);
		bean.setIntro(intro);
		bean.setMind_star(mind_star);
		bean.setMoney_star(money_star);
		bean.setHealth_star(health_star);
		bean.setLuck_dir(luck_dir);
		bean.setLove_star(love_star);
		bean.setWork_star(work_star);
		bean.setLuck_color(luck_color);
		bean.setLuck_thing(luck_thing);
		bean.setMind_word(mind_word);
		bean.setLove_word(love_word);
		bean.setMind_word(mind_word);
		bean.setWork_word(work_word);
		session.save(bean);
//		tx.commit();
		
		
	}

	@Override
	public void update(String name, byte[] pic, String intro, String mind_star,
			String money_star, String health_star, String luck_dir,
			String love_star, String work_star, String luck_color,
			String luck_thing, String mind_word, String love_word,
			String money_word, String work_word) {
		Session session = sessionFactory.getCurrentSession();
//		Transaction tx = session.getTransaction();
		ConstellationBean bean = (ConstellationBean)session.get(ConstellationBean.class, name);
		bean.setPic(pic);
		bean.setIntro(intro);
		bean.setMind_star(mind_star);
		bean.setMoney_star(money_star);
		bean.setHealth_star(health_star);
		bean.setLuck_dir(luck_dir);
		bean.setLove_star(love_star);
		bean.setWork_star(work_star);
		bean.setLuck_color(luck_color);
		bean.setLuck_thing(luck_thing);
		bean.setMind_word(mind_word);
		bean.setLove_word(love_word);
		bean.setMind_word(mind_word);
		bean.setWork_word(work_word);
		session.update(bean);
//		tx.commit();
	}

	@Override
	public void delete(String name) {
		Session session = sessionFactory.getCurrentSession();
		// Transaction tx= session.beginTransaction();
		ConstellationBean bean = (ConstellationBean) session.get(
				ConstellationBean.class, name);
		session.delete(bean);
		// tx.commit();
		// session.close();

	}

	public static void main(String[] args) throws Exception {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"beans.config.xml");

		SessionFactory session = (SessionFactory) context
				.getBean("sessionFactory");

		Transaction tx = session.getCurrentSession().beginTransaction();

		ConstellationHibernateDAO cdao = (ConstellationHibernateDAO) context
				.getBean("ConstellationDAO");
		
		//圖片寫入
//		File originalImgFile = new File("E:/資策會/星座照片/12.gif");
//		BufferedImage bufferedImage = ImageIO.read(originalImgFile);
//		ByteArrayOutputStream baos = new ByteArrayOutputStream();
//		ImageIO.write(bufferedImage, "gif", baos);
//		baos.flush();
//		byte[] originalImgByte = baos.toByteArray();
//		baos.close();
		
		
//		ConstellationBean bean = cdao.select("雙魚座");
//		bean.setPic(originalImgByte);
//		cdao.update(bean.getName(), bean.getPic(), bean.getIntro(), bean.getMind_star(), bean.getMoney_star(), bean.getHealth_star(), bean.getLuck_dir(), bean.getLove_star(), bean.getWork_star(), bean.getLuck_color(), bean.getLuck_thing(), bean.getMind_word(), bean.getLove_word(), bean.getMoney_word(), bean.getWork_word());
//		cdao.insertConstellation("雙魚座", null, null, null, null, null, null, null, null, null, null, null, null, null, null);
		
		
		
		
		System.out.println("done");
		
		
		//cdao.insertConstellation("hf", null , "8");
		//cdao.update("hf", null, "6");
		//cdao.select("kk");
		//cdao.delete("hf");

		tx.commit();

	}

	

	

}
